package com.ruoyi.dynamic.controller.app;


import com.ruoyi.common.core.constant.constant.PathConstant;
import com.ruoyi.common.core.context.SecurityContextHolder;
import com.ruoyi.common.core.pojo.result.Pages;
import com.ruoyi.common.core.pojo.vo.BaseVO;
import com.ruoyi.common.core.pojo.vo.PageVO;
import com.ruoyi.common.core.pojo.vo.SingleVO;
import com.ruoyi.common.core.result.Result;
import com.ruoyi.dynamic.model.constant.DynamicConstant;
import com.ruoyi.dynamic.model.pojo.entity.RoomAuction;
import com.ruoyi.dynamic.model.pojo.request.app.AppRoomAuctionCheckRequest;
import com.ruoyi.dynamic.model.pojo.request.app.AppRoomAuctionSearchRequest;
import com.ruoyi.dynamic.model.pojo.request.app.AppRoomAuctionStartRequest;
import com.ruoyi.dynamic.model.pojo.request.app.AppRoomChumApplyRequest;
import com.ruoyi.dynamic.model.pojo.result.app.AppAuctionInfoResult;
import com.ruoyi.dynamic.model.pojo.result.app.AppRoomAuctionGiftResult;
import com.ruoyi.dynamic.model.pojo.result.app.AppRoomAuctionResult;
import com.ruoyi.dynamic.service.IRoomAuctionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@Tag(name = "App-房间心动拍拍相关", description = "App-房间心动拍拍相关")
@RestController
@RequestMapping(PathConstant.APP_PATH + "room-chum")
public class AppRoomAuctionController {

    @Autowired
    private IRoomAuctionService iRoomAuctionService;

    @Operation(summary = "申请拍拍", description = "申请拍拍")
    @PostMapping("applyChum")
    public BaseVO applyChum(@RequestBody @Validated AppRoomChumApplyRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        boolean apply = iRoomAuctionService.applyChum(request, userId);
        return Result.successOrFail(apply);
    }

    @Operation(summary = "拍卖列表/审核列表", description = "拍卖列表/审核列表")
    @GetMapping("listPage")
    public PageVO<AppRoomAuctionResult> listPage(@ParameterObject @Validated AppRoomAuctionSearchRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        Pages<AppRoomAuctionResult> pages = iRoomAuctionService.listPage(request, userId);
        return Result.page(pages);
    }

    @Operation(summary = "撤回申请", description = "撤回申请")
    @GetMapping("cancel")
    public BaseVO cancel(Long roomId) {
        Long userId = SecurityContextHolder.getUserId();
        return Result.successOrFail(iRoomAuctionService.lambdaUpdate()
                .eq(RoomAuction::getRoomId, roomId)
                .eq(RoomAuction::getUserId, userId)
                .eq(RoomAuction::getStatus, DynamicConstant.CheckStatus.NO)
                .remove());
    }

    @Operation(summary = "拍卖审核", description = "拍卖审核")
    @PostMapping("check")
    public BaseVO check(@RequestBody @Validated AppRoomAuctionCheckRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        return Result.successOrFail(iRoomAuctionService.check(request, userId));
    }

    @Operation(summary = "开始拍卖", description = "开始拍卖")
    @PostMapping("start")
    public BaseVO start(@RequestBody @Validated AppRoomAuctionStartRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        iRoomAuctionService.startAuction(userId, request);
        return Result.success();
    }

    @Operation(summary = "赠送礼物-查看与第一名差距", description = "赠送礼物-查看与第一名差距")
    @PostMapping("lookDiff")
    public SingleVO<AppRoomAuctionGiftResult> lookDiff(@RequestBody @Validated AppRoomAuctionStartRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        AppRoomAuctionGiftResult result = iRoomAuctionService.lookDiff(userId, request);
        return Result.single(result);
    }

    @Operation(summary = "当前拍拍详情", description = "当前拍拍详情")
    @GetMapping("info")
    public SingleVO<AppAuctionInfoResult> info(Long id) {
        return Result.single(iRoomAuctionService.info(id, SecurityContextHolder.getUserId()));
    }


}

